﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class PointRate
    {
        public Chain.Model.PointRate DataRowToModel(DataRow row)
        {
            Chain.Model.PointRate rate = new Chain.Model.PointRate();
            if (row != null)
            {
                if ((row["PointRateID"] != null) && (row["PointRateID"].ToString() != ""))
                {
                    rate.PointRateID = int.Parse(row["PointRateID"].ToString());
                }
                if ((row["PointRateType"] != null) && (row["PointRateType"].ToString() != ""))
                {
                    if ((row["PointRateType"].ToString() == "1") || (row["PointRateType"].ToString().ToLower() == "true"))
                    {
                        rate.PointRateType = true;
                    }
                    else
                    {
                        rate.PointRateType = false;
                    }
                }
                if ((row["PointRateLevel"] != null) && (row["PointRateLevel"].ToString() != ""))
                {
                    rate.PointRateLevel = int.Parse(row["PointRateLevel"].ToString());
                }
                if ((row["MemLevel1"] != null) && (row["MemLevel1"].ToString() != ""))
                {
                    rate.MemLevel1 = decimal.Parse(row["MemLevel1"].ToString());
                }
                if ((row["MemLevel2"] != null) && (row["MemLevel2"].ToString() != ""))
                {
                    rate.MemLevel2 = decimal.Parse(row["MemLevel2"].ToString());
                }
                if ((row["MemLevel3"] != null) && (row["MemLevel3"].ToString() != ""))
                {
                    rate.MemLevel3 = decimal.Parse(row["MemLevel3"].ToString());
                }
                if ((row["MemLevel4"] != null) && (row["MemLevel4"].ToString() != ""))
                {
                    rate.MemLevel4 = decimal.Parse(row["MemLevel4"].ToString());
                }
                if ((row["MemLevel5"] != null) && (row["MemLevel5"].ToString() != ""))
                {
                    rate.MemLevel5 = decimal.Parse(row["MemLevel5"].ToString());
                }
                if ((row["MemLevel6"] != null) && (row["MemLevel6"].ToString() != ""))
                {
                    rate.MemLevel6 = decimal.Parse(row["MemLevel6"].ToString());
                }
                if ((row["MemLevel7"] != null) && (row["MemLevel7"].ToString() != ""))
                {
                    rate.MemLevel7 = decimal.Parse(row["MemLevel7"].ToString());
                }
                if ((row["MemLevel8"] != null) && (row["MemLevel8"].ToString() != ""))
                {
                    rate.MemLevel8 = decimal.Parse(row["MemLevel8"].ToString());
                }
                if ((row["MemLevel9"] != null) && (row["MemLevel9"].ToString() != ""))
                {
                    rate.MemLevel9 = decimal.Parse(row["MemLevel9"].ToString());
                }
                if ((row["MemLevel10"] != null) && (row["MemLevel10"].ToString() != ""))
                {
                    rate.MemLevel10 = decimal.Parse(row["MemLevel10"].ToString());
                }
                if ((row["MemLevel11"] != null) && (row["MemLevel11"].ToString() != ""))
                {
                    rate.MemLevel11 = decimal.Parse(row["MemLevel11"].ToString());
                }
                if ((row["MemLevel12"] != null) && (row["MemLevel12"].ToString() != ""))
                {
                    rate.MemLevel12 = decimal.Parse(row["MemLevel12"].ToString());
                }
                if ((row["MemLevel13"] != null) && (row["MemLevel13"].ToString() != ""))
                {
                    rate.MemLevel13 = decimal.Parse(row["MemLevel13"].ToString());
                }
                if ((row["MemLevel14"] != null) && (row["MemLevel14"].ToString() != ""))
                {
                    rate.MemLevel14 = decimal.Parse(row["MemLevel14"].ToString());
                }
                if ((row["MemLevel15"] != null) && (row["MemLevel15"].ToString() != ""))
                {
                    rate.MemLevel15 = decimal.Parse(row["MemLevel15"].ToString());
                }
            }
            return rate;
        }

        public DataRow GetDataRow()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 * from PointRate");
            DataSet set = DbHelperSQL.Query(builder.ToString());
            if (set.Tables[0].Rows.Count > 0)
            {
                return set.Tables[0].Rows[0];
            }
            return null;
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, string strTime, params string[] strWhere)
        {
            string[] strArray;
            string tableName = "MEM ,SYSSHOP,MEMLEVEL,SYSUSER";
           // strArray = new string[] { strArray[0].Replace("and 1=1 ", strTime) };
            strArray = new string[] { ("and 1=1 ") };
            strWhere[0] = strWhere[0].Substring(0x20);
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, strArray, condition, "MemID", "MemID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("PointRateID", "PointRate");
        }

        public DataSet GetMemDetailByMemCard(int MemID, string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select PointLog.*,Mem.MemCard,Mem.MemName ");
            builder.Append(" from PointLog,Mem ");
            builder.Append(" where PointLog.PointMemID = Mem.MemID ");
            builder.Append(" and PointMemID=" + MemID + " and PointChangeType in (9,12)");
            if (strWhere != "")
            {
                builder.Append(strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMemDetailByMemID(int MemID, string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select PointLog.*,Mem.MemCard,Mem.MemName ");
            builder.Append(" from PointLog,Mem ");
            builder.Append(" where PointLog.PointGiveMemID = Mem.MemID ");
            builder.Append(" and PointMemID=" + MemID + " and PointChangeType in (9,12)");
            if (strWhere != "")
            {
                builder.Append(strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMyTeamList(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = " V_MyTeam";
            string[] column = new string[] { "Rowid,SumNumber,MemCard,MemName,MemMobile,PointGiveMemID " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "Rowid", "Rowid", true, PageSize, PageIndex, recordCount);
            try
            {
                resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            }
            catch
            {
                resCount = 0;
            }
            return set;
        }

        public DataSet GetPointLog(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM V_PointLog ");
            if (strWhere != "")
            {
                builder.Append("WHERE " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.PointRate GetPointRate()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 * from PointRate");
            DataSet set = DbHelperSQL.Query(builder.ToString());
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetPointRateNumber(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(PointNumber) FROM PointLog,Mem,MemLevel,SysShop,SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.PointRate model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update PointRate set ");
            builder.Append("PointRateType=@PointRateType,");
            builder.Append("MemLevel1=@MemLevel1,");
            builder.Append("MemLevel2=@MemLevel2,");
            builder.Append("MemLevel3=@MemLevel3,");
            builder.Append("MemLevel4=@MemLevel4,");
            builder.Append("MemLevel5=@MemLevel5,");
            builder.Append("MemLevel6=@MemLevel6,");
            builder.Append("MemLevel7=@MemLevel7,");
            builder.Append("MemLevel8=@MemLevel8,");
            builder.Append("MemLevel9=@MemLevel9,");
            builder.Append("MemLevel10=@MemLevel10,");
            builder.Append("MemLevel11=@MemLevel11,");
            builder.Append("MemLevel12=@MemLevel12,");
            builder.Append("MemLevel13=@MemLevel13,");
            builder.Append("MemLevel14=@MemLevel14,");
            builder.Append("MemLevel15=@MemLevel15,");
            builder.Append("PointRateLevel=@PointRateLevel");
            builder.Append(" where PointRateID=@PointRateID");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@PointRateType", SqlDbType.Bit, 4), new SqlParameter("@MemLevel1", SqlDbType.Float, 6), new SqlParameter("@MemLevel2", SqlDbType.Float, 6), new SqlParameter("@MemLevel3", SqlDbType.Float, 6), new SqlParameter("@MemLevel4", SqlDbType.Float, 6), new SqlParameter("@MemLevel5", SqlDbType.Float, 6), new SqlParameter("@MemLevel6", SqlDbType.Float, 6), new SqlParameter("@MemLevel7", SqlDbType.Float, 6), new SqlParameter("@MemLevel8", SqlDbType.Float, 6), new SqlParameter("@MemLevel9", SqlDbType.Float, 6), new SqlParameter("@MemLevel10", SqlDbType.Float, 6), new SqlParameter("@MemLevel11", SqlDbType.Float, 6), new SqlParameter("@MemLevel12", SqlDbType.Float, 6), new SqlParameter("@MemLevel13", SqlDbType.Float, 6), new SqlParameter("@MemLevel14", SqlDbType.Float, 6), new SqlParameter("@MemLevel15", SqlDbType.Float, 6), 
                new SqlParameter("@PointRateLevel", SqlDbType.Int, 4), new SqlParameter("@PointRateID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.PointRateType;
            cmdParms[1].Value = model.MemLevel1;
            cmdParms[2].Value = model.MemLevel2;
            cmdParms[3].Value = model.MemLevel3;
            cmdParms[4].Value = model.MemLevel4;
            cmdParms[5].Value = model.MemLevel5;
            cmdParms[6].Value = model.MemLevel6;
            cmdParms[7].Value = model.MemLevel7;
            cmdParms[8].Value = model.MemLevel8;
            cmdParms[9].Value = model.MemLevel9;
            cmdParms[10].Value = model.MemLevel10;
            cmdParms[11].Value = model.MemLevel11;
            cmdParms[12].Value = model.MemLevel12;
            cmdParms[13].Value = model.MemLevel13;
            cmdParms[14].Value = model.MemLevel14;
            cmdParms[15].Value = model.MemLevel15;
            cmdParms[0x10].Value = model.PointRateLevel;
            cmdParms[0x11].Value = model.PointRateID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

